package com.typing.battle.util;

import java.text.*;
import java.util.*;

public class StrUtil {
public static final String TAG ="StrUtil";
	/**
	 *  문자열 예외체크
	 */
    public static String getStr(String str){
    	KLog.d(StrUtil.class,"getStr",str);
    	return str==null?"": str.trim();
    }    

	/**
	 *  정수형 변수 예외체크
	 */
    public static int getInt(String str){
        try{
            return Integer.parseInt(str);
        }catch (Exception e) {
            return 0;
        }
    }
    /**
     *  Long 변수 예외체크
     */
    public static long getLong(String str){
    	try{
    		return Long.parseLong(str);
    	}catch (Exception e) {
    		return 0L;
    	}
    }
    
	// 날짜변환 -> 'Month ,2011'
	public static String getDateSubTitle(String datestring) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		String result="";
		Format formatter = null;
		try {
			Date date = sdf.parse(datestring);
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			if(Locale.getDefault().getCountry().equals("KR")){
				formatter = new SimpleDateFormat("MMMM"); // 월
				result = String.format("%tY년 ", cal)+formatter.format(date);
			}else{			
				formatter = new SimpleDateFormat("MMM"); // Jan
				result = formatter.format(date)+String.format(", %tY", cal);
			}
			return result;
		} catch (Exception e) {
			return result;
		}
	}
	
	// 날짜변환 -> 'yyyy.mm.dd'
	public static String getDate(String datestring) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String result="";
		try {
			Date date = sdf.parse(datestring);
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			result = String.format("%tY.%tm.%td", cal,cal,cal);
			return result;
		} catch (Exception e) {
			return result;
		}
	}
	
	public static String getStrFilter(String str) {
		if (StrUtil.getStr(str).equals("")) {
			return "Unknown";
		}
		
		int ascNum = str.toUpperCase().charAt(0);
		
		String returnStr = "";
		if (ascNum >= 48 && 57 >= ascNum) { 
			returnStr = "123.."; // 숫자
		} else if (ascNum >= 65 && 90 >= ascNum) {
			returnStr = str.substring(0, 1); // 영어
		} else {
 			returnStr = "etc";
		}
		return returnStr;
	}

}
